﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_PhuongThucThanhToan;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiHoaDon;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiMuaSanPham;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiTienTe;
using csla4Skeleton.UI.SL.DanhMuc.DM_HangHoa;
using csla4Skeleton.UI.SL.NghiepVu.TienMat.TM_PhieuThu;
using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.DanhMuc.DM_DonViTinh;
using csla4Skeleton.UI.SL.DanhMuc.DM_ThueSuat;
using csla4Skeleton.UI.SL.DanhMuc.DM_TinhChatHangHoa;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;
using Telerik.Windows.Data;
using ConfigurationNghiepVu;
using Telerik.Windows.Controls.GridView.Columns;
using System.Windows.Media;
namespace csla4Skeleton.UI.SL.NghiepVu.BanSanPham.BSP_BanDichVu
{
    public partial class BSP_BanDichVu_MainWindow : UserControl, Csla.Xaml.ISupportNavigation
    {
        #region ISupportNavigation Members
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        /// <summary>
        /// Set parameter for user control
        /// </summary>
        /// <param name="parameters">[Khoa];[DaGhiSo||ChuaGhiSo];[MaHoaDonBan]</param>
        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                {
                    Khoa = null;
                    MaHoaDonBan = "";
                }
                else
                {
                    Khoa = Int32.Parse((parameters.Split(';')[0]));
                    DaGhiSo = parameters.Split(';')[1] == "DaGhiSo";
                    MaHoaDonBan = parameters.Split(';')[2];
                }
            }
            catch
            {
                Khoa = null;
                MaHoaDonBan = "";
            }
        }

        public string Title
        {
            get { return "BanDichVu"; }
        }
        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
        private bool DaGhiSo = false;
        private string MaHoaDonBan = "";
        DM_TaiKhoan_ListViewModel DM_TaiKhoan_ListViewModel;

        public BSP_BanDichVu_MainWindow(int Khoa)
        {
            this.Khoa = Khoa;
        }

        public BSP_BanDichVu_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.BanSanPham.BSP_BanDichVu.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);

            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource BSP_BanDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["BSP_BanDichVu_ViewModelSource"];
                BSP_BanDichVu_ViewModel BSP_BanDichVu_ViewModel;
                if (Khoa == null)
                {
                    BSP_BanDichVu_ViewModel = new BSP_BanDichVu_ViewModel();
                }
                else
                {
                    BSP_BanDichVu_ViewModel = new BSP_BanDichVu_ViewModel((int)Khoa);
                }

                BSP_BanDichVu_ViewModel.LoadCompleted += new BSP_BanDichVu_ViewModel.LoadCompleted_EventHandler(BSP_BanDichVu_ViewModel_LoadCompleted);

                DM_TaiKhoan_ListViewModel = new DM_TaiKhoan_ListViewModel
                    (DanhMucTaiKhoanDefault.DanhMuc_TaiKhoan(BusinessConfiguration.Configuration.LoaiChungTuId_BSP_BanDichVu));
                DM_TaiKhoan_ListViewModel.LoadCompleted += new EventHandler(DM_TaiKhoan_ListViewModel_LoadCompleted);

                BSP_BanDichVu_ViewModel.ChildList_AddedNew += new BSP_BanDichVu_ViewModel.ChildList_AddedNew_EventHandler(BSP_BanDichVu_ViewModel_ChildList_AddedNew);

                BSP_BanDichVu_ViewModel.LoaiTienTeChanged += new EventHandler(BSP_BanDichVu_LoaiTienTeChanged);
                BSP_BanDichVu_ViewModel.ThueSuatIdChanged += new BSP_BanDichVu_ViewModel.ThueSuatIdChanged_EventHandler(BSP_BanDichVu_ThueSuatIdChanged);
                BSP_BanDichVu_ViewModel.DieuKienNghiepVuChanged += new BSP_BanDichVu.BSP_BanDichVu_ViewModel.DieuKienNghiepVuChanged_EventHandler(BSP_BanDichVu_DieuKienNghiepVuChanged);
                BSP_BanDichVu_CollectionViewSource.Source = new List<object> { BSP_BanDichVu_ViewModel };

                System.Windows.Data.CollectionViewSource dm_NhaCungCap_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel dm_NhaCungCap_ListViewModel = new DM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_KhachHang);
                dm_NhaCungCap_CollectionViewSource.Source = new List<object> { dm_NhaCungCap_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_PhuongThucThanhToan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_PhuongThucThanhToan_ListViewModelSource"];
                DM_PhuongThucThanhToan_ListViewModel dm_PhuongThucThanhToan_ListViewModel = new DM_PhuongThucThanhToan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_PhuongThucThanhToan_CollectionViewSource.Source = new List<object> { dm_PhuongThucThanhToan_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiHoaDon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiHoaDon_ListViewModelSource"];
                DM_LoaiHoaDon_ListViewModel dm_LoaiHoaDon_ListViewModel = new DM_LoaiHoaDon_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiHoaDon_CollectionViewSource.Source = new List<object> { dm_LoaiHoaDon_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
                DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = new DM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiTienTe_CollectionViewSource.Source = new List<object> { dm_LoaiTienTe_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_DonViTinh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DonViTinh_ListViewModelSource"];
                DM_DonViTinh_ListViewModel dm_DonViTinh_ListViewModel = new DM_DonViTinh_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DonViTinh_CollectionViewSource.Source = new List<object> { dm_DonViTinh_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
                DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = new DM_HangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_HangHoa_CollectionViewSource.Source = new List<object> { dm_HangHoa_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
                DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = new DM_ThueSuat_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_ThueSuat_CollectionViewSource.Source = new List<object> { dm_ThueSuat_ListViewModel };
            }
        }
        #endregion

        #region Event Handle
        private RadGridView GetActiveGridView()
        {
            if (this.BSP_BanDichVu_BanDichVuChiTietTab.IsSelected)
            {
                return BSP_BanDichVu_BanDichVuChiTietGridView;
            }
            else if (this.BSP_BanDichVu_ThueChiTietTab.IsSelected)
            {
                return BSP_BanDichVu_ThueChiTietGridView;
            }
            return null;
        }

        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                BSP_BanDichVu_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave && CheckBeforeSave())
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.BSP_BanDichVu_BanDichVuChiTietTab.IsSelected = true;
                    FocusAfterChangeTab(this.BSP_BanDichVu_BanDichVuChiTietGridView);
                }
                else if (e.Key == Key.D2)
                {
                    this.BSP_BanDichVu_ThueChiTietTab.IsSelected = true;
                    FocusAfterChangeTab(this.BSP_BanDichVu_ThueChiTietGridView);
                }
            }
            e.Handled = true;
        }

        private void FocusAfterChangeTab(GridViewDataControl grid)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                GridViewUtil.FocusLastRow((RadGridView)grid);
                grid.Focus();
                grid.BeginEdit();
            }));
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();
        }

        public void AddItems()
        {
            if (this.BSP_BanDichVu_BanDichVuChiTietTab.IsSelected)
            {
                AddBanDichVuDetailsItem();
            }
        }

        void DeleteItems()
        {
            if (this.BSP_BanDichVu_BanDichVuChiTietTab.IsSelected)
            {
                BSP_BanDichVu_ChiTietChild ChiTietChild = (BSP_BanDichVu_ChiTietChild)(this.BSP_BanDichVu_BanDichVuChiTietGridView.SelectedItem);
                IList<BSP_BanDichVu_ChiTietChild> list = GetViewModel().Model.BSP_BanDichVu_ChiTiets;
                int index = 0;
                foreach (BSP_BanDichVu_ChiTietChild item in list)
                {
                    if (ChiTietChild.GetHashCode() == item.GetHashCode())
                    {
                        break;
                    }
                    index++;
                }

                DeleteBanDichVuDetailsItem(index);
            }
        }

        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }

        }

        private void BSP_BanDichVu_ViewModel_LoadCompleted(object sender, LoadedEventArgs e)
        {
            if (e.LoadMode == LoadModeEnum.ForNew)
            {
                if (DaGhiSo || MaHoaDonBan != "")
                {
                    string message = "";
                    if (DaGhiSo)
                    {
                        message += "Chứng từ đã ghi sổ. Bỏ ghi để có thể chỉnh sửa thông tin ghi sổ\n";
                    }
                    if (MaHoaDonBan != "")
                    {
                        message += "Đã phân bổ chi phí ở hóa đơn " + MaHoaDonBan + ". Không được phép chỉnh sửa";
                    }

                    GetViewModel().Model.ErrorMessage = message;
                    SetEnableForUserControls(ContentRoot, true);

                    this.BSP_BanDichVu_BanDichVuChiTietGridView.IsReadOnly = true;
                    this.BSP_BanDichVu_ThueChiTietGridView.IsReadOnly = true;
                }
            }
            else if (e.LoadMode == LoadModeEnum.ForBoGhi)
            {
                string message = "Bỏ ghi thành công. Chứng từ có thể chính sửa\n";
                GetViewModel().Model.ErrorMessage = message;

                //Cap nhat lai danh muc nha cung cap
                System.Windows.Data.CollectionViewSource dm_NhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel vm = ((List<object>)dm_NhaCungCap_ListViewSource.Source)[0] as DM_NhaCungCap_ListViewModel;
                vm.GetDM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_KhachHang);

                SetEnableForUserControls(ContentRoot, false);

                this.BSP_BanDichVu_BanDichVuChiTietGridView.IsReadOnly = false;
                this.BSP_BanDichVu_ThueChiTietGridView.IsReadOnly = false;
            }
            else if (e.LoadMode == LoadModeEnum.ForSave)
            {
                //Cap nhat lai danh muc nha cung cap
                System.Windows.Data.CollectionViewSource dm_NhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel vm = ((List<object>)dm_NhaCungCap_ListViewSource.Source)[0] as DM_NhaCungCap_ListViewModel;
                vm.GetDM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_KhachHang);

                if (DaGhiSo || MaHoaDonBan != "")
                {
                    string message = "";
                    if (DaGhiSo)
                    {
                        message += "Chứng từ đã ghi sổ. Bỏ ghi để có thể chỉnh sửa thông tin ghi sổ\n";
                    }
                    if (MaHoaDonBan != "")
                    {
                        message += "Đã phân bổ chi phí ở hóa đơn " + MaHoaDonBan + ". Không được phép chỉnh sửa";
                    }

                    GetViewModel().Model.ErrorMessage = message;
                    SetEnableForUserControls(ContentRoot, true);

                    this.BSP_BanDichVu_BanDichVuChiTietGridView.IsReadOnly = true;
                    this.BSP_BanDichVu_ThueChiTietGridView.IsReadOnly = true;
                }
            }
        }

        private void SetEnableForUserControls(DependencyObject root, bool disableMode)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(root, i);
                if (child is Control)
                {
                    Control control = child as Control;
                    if (disableMode)
                    {
                        if (control == this.GoBackToParentListButton)
                            control.IsEnabled = true;
                        else if (control == this.tabControl)
                            control.IsEnabled = true;
                        else
                        {
                            if (DaGhiSo)
                            {
                                if (control == this.BoGhiButton)
                                    control.IsEnabled = true;
                                else if (control == this.BSP_BanDichVu_MoreInformation_LaChiPhiBanHang_CheckBox)
                                    control.IsEnabled = true;
                                else if (control == this.SaveItemButton)
                                    control.IsEnabled = true;
                                else if (control == this.SaveNewItemButton)
                                    control.IsEnabled = true;
                                else
                                    control.IsEnabled = false;
                            }
                            else
                            {
                                control.IsEnabled = false;
                            }
                        }
                    }
                    else
                    {
                        if (control == this.BoGhiButton)
                            control.IsEnabled = false;
                        else
                            control.IsEnabled = true;
                    }
                }
                else
                {
                    SetEnableForUserControls(child, disableMode);
                }
            }
        }

        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
            if (CheckBeforeSave())
                this.GetViewModel().SaveAndNew();
        }

        private void BoGhiButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().BoGhi();
        }

        private void BSP_BanDichVu_GeneralInformation_NhaCungCapId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_NhaCungCap_ChildWindow dm_NhaCungCap_ChildWindow = new DM_NhaCungCap_ChildWindow();
            dm_NhaCungCap_ChildWindow.Show();
            dm_NhaCungCap_ChildWindow.SaveClicked += new EventHandler(dm_NhaCungCap_ViewModel_Saved);
        }

        private void BSP_BanDichVu_MoreInformation_PhuongThucThanhToanId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_PhuongThucThanhToan_ChildWindow dm_PhuongThucThanhToan_ChildWindow = new DM_PhuongThucThanhToan_ChildWindow();
            dm_PhuongThucThanhToan_ChildWindow.Show();
            dm_PhuongThucThanhToan_ChildWindow.SaveClicked += new EventHandler(dm_PhuongThucThanhToan_ViewModel_Saved);
        }

        private void BSP_BanDichVu_MoreInformation_LoaiTienId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiTienTe_ChildWindow dm_LoaiTienTe_ChildWindow = new DM_LoaiTienTe_ChildWindow();
            dm_LoaiTienTe_ChildWindow.Show();
            dm_LoaiTienTe_ChildWindow.SaveClicked += new EventHandler(dm_LoaiTienTe_ViewModel_Saved);
        }

        private void BSP_BanDichVu_MoreInformation_LoaiBanSanPhamId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiMuaSanPham_ChildWindow dm_LoaiBanSanPham_ChildWindow = new DM_LoaiMuaSanPham_ChildWindow();
            dm_LoaiBanSanPham_ChildWindow.Show();
            dm_LoaiBanSanPham_ChildWindow.SaveClicked += new EventHandler(dm_LoaiBanSanPham_ViewModel_Saved);
        }

        private void BSP_BanDichVu_MoreInformation_LoaiHoaDonId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiHoaDon_ChildWindow dm_LoaiHoaDon_ChildWindow = new DM_LoaiHoaDon_ChildWindow();
            dm_LoaiHoaDon_ChildWindow.Show();
            dm_LoaiHoaDon_ChildWindow.SaveClicked += new EventHandler(dm_LoaiHoaDon_ViewModel_Saved);
        }
        #endregion

        #region View Model Related Methods

        private BSP_BanDichVu_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource BSP_BanDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["BSP_BanDichVu_ViewModelSource"];
            return ((List<object>)BSP_BanDichVu_CollectionViewSource.Source)[0] as BSP_BanDichVu_ViewModel;
        }

        void dm_NhaCungCap_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
            DM_NhaCungCap_ListViewModel vm = ((List<object>)dm_NhaCungCap_ListViewSource.Source)[0] as DM_NhaCungCap_ListViewModel;
            vm.GetDM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_KhachHang);
        }

        void dm_LoaiTienTe_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiTienTe_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
            DM_LoaiTienTe_ListViewModel vm = ((List<object>)dm_LoaiTienTe_ListViewSource.Source)[0] as DM_LoaiTienTe_ListViewModel;
            vm.GetDM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_LoaiHoaDon_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiHoaDon_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiHoaDon_ListViewModelSource"];
            DM_LoaiHoaDon_ListViewModel vm = ((List<object>)dm_LoaiHoaDon_ListViewSource.Source)[0] as DM_LoaiHoaDon_ListViewModel;
            vm.GetDM_LoaiHoaDon_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_LoaiBanSanPham_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiBanSanPham_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiMuaSanPham_ListViewModelSource"];
            DM_LoaiMuaSanPham_ListViewModel vm = ((List<object>)dm_LoaiBanSanPham_ListViewSource.Source)[0] as DM_LoaiMuaSanPham_ListViewModel;
            vm.GetDM_LoaiMuaSanPham_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_PhuongThucThanhToan_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_PhuongThucThanhToan_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_PhuongThucThanhToan_ListViewModelSource"];
            DM_PhuongThucThanhToan_ListViewModel vm = ((List<object>)dm_PhuongThucThanhToan_ListViewSource.Source)[0] as DM_PhuongThucThanhToan_ListViewModel;
            vm.GetDM_PhuongThucThanhToan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void BSP_BanDichVu_ThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
            DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = ((List<object>)dm_ThueSuat_CollectionViewSource.Source)[0] as DM_ThueSuat_ListViewModel;

            BSP_BanDichVu_ThueChild ThueChild = GetViewModel().Model.BSP_BanDichVu_Thues[e.Index];
            IList<DM_ThueSuatInfo> list = dm_ThueSuat_ListViewModel.Model.getListOfInfo();
            foreach (DM_ThueSuatInfo item in list)
            {
                if (item.Khoa == e.ThueSuatId)
                {
                    ThueChild.ThueSuatValue = (double)item.GiaTriTinhThue;
                }
            }
        }

        void BSP_BanDichVu_LoaiTienTeChanged(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
            DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = ((List<object>)dm_LoaiTienTe_CollectionViewSource.Source)[0] as DM_LoaiTienTe_ListViewModel;
            IList<DM_LoaiTienTeInfo> list = dm_LoaiTienTe_ListViewModel.Model.getListOfInfo();
            foreach (DM_LoaiTienTeInfo item in list)
            {
                if (item.Khoa == GetViewModel().Model.LoaiTienId)
                {
                    GetViewModel().Model.TyGia = item.TyGiaQuyDoi;
                    break;
                }
            }
        }

        private void AddBanDichVuDetailsItem()
        {
            GetViewModel().AddBSP_BanDichVu_ChiTietItem();
        }

        private void DeleteBanDichVuDetailsItem(int Index)
        {
            GetViewModel().DeleteBSP_BanDichVu_ChiTietItem(Index);
        }

        void BSP_BanDichVu_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "BSP_BanDichVu_ChiTiets")
            {
                this.BSP_BanDichVu_BanDichVuChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
                GetViewModel().Model.BSP_BanDichVu_ChiTiets[GetViewModel().Model.BSP_BanDichVu_ChiTiets.Count - 1].TKCoId
                    = LayTaiKhoanCo_ChiTietId_DauTien_ComboBox();
                GetViewModel().Model.BSP_BanDichVu_ChiTiets[GetViewModel().Model.BSP_BanDichVu_ChiTiets.Count - 1].TKNoId
                    = LayTaiKhoanNo_ChiTietId_DauTien_ComboBox();
            }
            else if (e.ChildListName == "BSP_BanDichVu_Thues")
            {
                GetViewModel().Model.BSP_BanDichVu_Thues[GetViewModel().Model.BSP_BanDichVu_Thues.Count - 1].TKNoId
                    = LayTaiKhoanNo_ThueId_DauTien_ComboBox();
            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.GetViewModel().CanSave && CheckBeforeSave())
            {
                this.GetViewModel().SaveBanDichVu();
            }
        }

        /// <summary>
        /// Kiem tra han muc cong no
        /// </summary>
        /// <returns></returns>
        private bool CheckBeforeSave()
        {
            bool result = true;

            int? hanNo =
                ((DM_NhaCungCapInfo)BSP_BanDichVu_GeneralInformation_NhaCungCapId_ComboBox.SelectedItem).HanNo;
            hanNo = hanNo == null ? 0 : hanNo;

            double? dinhMucCongNo =
                ((DM_NhaCungCapInfo)BSP_BanDichVu_GeneralInformation_NhaCungCapId_ComboBox.SelectedItem).DinhMucCongNo;
            dinhMucCongNo = dinhMucCongNo == null ? 0 : dinhMucCongNo;

            double? dinhMucCongNoDaDung =
                ((DM_NhaCungCapInfo)BSP_BanDichVu_GeneralInformation_NhaCungCapId_ComboBox.SelectedItem).DinhMucCongNoDaDung;
            dinhMucCongNoDaDung = dinhMucCongNoDaDung == null ? 0 : dinhMucCongNoDaDung;

            dinhMucCongNoDaDung = dinhMucCongNoDaDung + (double)GetViewModel().Model.TongTienThanhToanQĐ;

            if (dinhMucCongNoDaDung > dinhMucCongNo)
            {
                MessageBox.Show
                    ("Định mức công nợ đã dùng vượt quá hạn mức công nợ.",
                    "Hệ thống thông tin kế toán", MessageBoxButton.OK);
                result = false;
            }

            return result;
        }
        #endregion

        #region Danh muc tai khoan

        private void DM_TaiKhoan_ListViewModel_LoadCompleted(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
            System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
            System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_Thue_ListViewModelSource"];
            System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_Thue_ListViewModelSource"];

            if (Khoa == null)
            {
                dm_TaiKhoanCo_ChiTiet_CollectionViewSource.Source = DM_TaiKhoan_ListViewModel.Model.getListOfInfo
                    (DanhMucTaiKhoanDefault.DanhMuc_TaiKhoanCo_ChiTiet(BusinessConfiguration.Configuration.LoaiChungTuId_BSP_BanDichVu));
                dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source = DM_TaiKhoan_ListViewModel.Model.getListOfInfo
                    (DanhMucTaiKhoanDefault.DanhMuc_TaiKhoanNo_ChiTiet(BusinessConfiguration.Configuration.LoaiChungTuId_BSP_BanDichVu));
                dm_TaiKhoanCo_Thue_CollectionViewSource.Source = DM_TaiKhoan_ListViewModel.Model.getListOfInfo
                    (DanhMucTaiKhoanDefault.DanhMuc_TaiKhoanCo_Thue(BusinessConfiguration.Configuration.LoaiChungTuId_BSP_BanDichVu));
                dm_TaiKhoanNo_Thue_CollectionViewSource.Source = DM_TaiKhoan_ListViewModel.Model.getListOfInfo
                    (DanhMucTaiKhoanDefault.DanhMuc_TaiKhoanNo_Thue(BusinessConfiguration.Configuration.LoaiChungTuId_BSP_BanDichVu));
            }
            else
            {
                dm_TaiKhoanCo_ChiTiet_CollectionViewSource.Source = DM_TaiKhoan_ListViewModel.Model.getListOfInfo();
                dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source = DM_TaiKhoan_ListViewModel.Model.getListOfInfo();
                dm_TaiKhoanCo_Thue_CollectionViewSource.Source = DM_TaiKhoan_ListViewModel.Model.getListOfInfo();
                dm_TaiKhoanNo_Thue_CollectionViewSource.Source = DM_TaiKhoan_ListViewModel.Model.getListOfInfo();
            }
        }

        void BSP_BanDichVu_DieuKienNghiepVuChanged(object sender, DieuKienNghiepVuEventArgs e)
        {
            if (DM_TaiKhoan_ListViewModel.Model != null)
            {
                System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
                dm_TaiKhoanCo_ChiTiet_CollectionViewSource.Source = DM_TaiKhoan_ListViewModel.Model.getListOfInfo(e.DanhMucTaiKhoanCoChiTiet);
                GetViewModel().Model.BSP_BanDichVu_ChiTiets.UpdateTKCoIdAllChild(LayTaiKhoanCo_ChiTietId_DauTien_ComboBox());

                System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
                dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source = DM_TaiKhoan_ListViewModel.Model.getListOfInfo(e.DanhMucTaiKhoanNoChiTiet);
                GetViewModel().Model.BSP_BanDichVu_ChiTiets.UpdateTKNoIdAllChild(LayTaiKhoanNo_ChiTietId_DauTien_ComboBox());

                System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_Thue_ListViewModelSource"];
                dm_TaiKhoanCo_Thue_CollectionViewSource.Source = DM_TaiKhoan_ListViewModel.Model.getListOfInfo(e.DanhMucTaiKhoanCoThue);

                System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_Thue_ListViewModelSource"];
                dm_TaiKhoanNo_Thue_CollectionViewSource.Source = DM_TaiKhoan_ListViewModel.Model.getListOfInfo(e.DanhMucTaiKhoanNoThue);
                GetViewModel().Model.BSP_BanDichVu_Thues.UpdateTKNoIdAllChild(LayTaiKhoanNo_ThueId_DauTien_ComboBox());
            }
        }

        int LayTaiKhoanCo_ChiTietId_DauTien_ComboBox()
        {
            System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
            return ((List<DM_TaiKhoanInfo>)dm_TaiKhoanCo_ChiTiet_CollectionViewSource.Source)[0].Khoa;
        }

        int LayTaiKhoanNo_ChiTietId_DauTien_ComboBox()
        {
            System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
            return ((List<DM_TaiKhoanInfo>)dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source)[0].Khoa;
        }

        int LayTaiKhoanCo_ThueId_DauTien_ComboBox()
        {
            System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_Thue_ListViewModelSource"];
            return ((List<DM_TaiKhoanInfo>)dm_TaiKhoanCo_Thue_CollectionViewSource.Source)[0].Khoa;
        }

        int LayTaiKhoanNo_ThueId_DauTien_ComboBox()
        {
            System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_Thue_ListViewModelSource"];
            return ((List<DM_TaiKhoanInfo>)dm_TaiKhoanNo_Thue_CollectionViewSource.Source)[0].Khoa;
        }
        #endregion
    }
}
